package com.study.compose.beta.view

import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonColors
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.module.compose.base.composable.CodeView
import com.module.compose.base.composable.Content
import com.module.compose.base.composable.ContentView
import com.module.compose.base.composable.Title
import com.module.compose.base.utils.ToastUtils
import com.study.compose.beta.theme.Orange
import com.study.compose.beta.theme.TextLightGray
import com.study.compose.beta.theme.White

private class CustomButtonColors : ButtonColors {

    @Composable
    override fun backgroundColor(enabled: Boolean): State<Color> {
        return rememberUpdatedState(if (enabled) Orange else TextLightGray)
    }

    @Composable
    override fun contentColor(enabled: Boolean): State<Color> {
        return rememberUpdatedState(if (enabled) White else Orange)
    }
}


@Composable
fun ButtonSample() {

    DisposableEffect(key1 = "ButtonSample") {

        Log.d("生命周期", "开始")

        onDispose {
            Log.d("生命周期", "结束")
        }
    }

    LazyColumn(
        Modifier
            .fillMaxSize()
            .padding(start = 15.dp, end = 15.dp)
    ) {
        item {
            Column {
                ContentView {
                    Title(title = "按钮控件Button，具有以下属性：")
                    Content(
                        content =
                        """
                        点击事件
                        onClick: () -> Unit,
                        设置属性
                        modifier: Modifier = Modifier,
                        是否可点击,默认效果会置灰
                        enabled: Boolean = true,
                        暂不清楚
                        interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
                        按钮的投影效果
                        elevation: ButtonElevation? = ButtonDefaults.elevation(),
                        定义按钮的形状
                        shape: Shape = MaterialTheme.shapes.small,
                        设置按钮的边框
                        border: BorderStroke? = null,
                        定义在不同状态下按钮的背景和内容的颜色
                        colors: ButtonColors = ButtonDefaults.buttonColors(),
                        设置内容边距
                        contentPadding: PaddingValues = ButtonDefaults.ContentPadding,
                        添加按钮的内容，compose按钮是一个容器
                        content: @Composable RowScope.() -> Unit
                        """.trimIndent()
                    )
                }
                Button(onClick = { }) {
                    Text(text = "一个简单的按钮", fontSize = 18.sp, color = White)
                }
                CodeView(
                    code =
                    """
                    Button(onClick = { ToastUtils.show("按钮点击") }) {
                        Text(text = "一个简单的按钮", fontSize = 18.sp, color = White)
                    }
                    """.trimIndent()
                )

                Button(onClick = { ToastUtils.show("按钮点击") }, shape = RoundedCornerShape(20.dp)) {
                    Text(text = "设置了shape的按钮", fontSize = 18.sp, color = White)
                }

                CodeView(
                    code =
                    """
                    Button(onClick = { ToastUtils.show("按钮点击") }, shape = RoundedCornerShape(20.dp)) {
                        Text(text = "设置了shape的按钮", fontSize = 18.sp, color = White)
                    }
                    """.trimIndent()
                )

                Button(
                    onClick = { ToastUtils.show("按钮点击") },
                    border = BorderStroke(width = 2.dp, color = Orange),
                ) {
                    Text(text = "设置了外边框的按钮", fontSize = 18.sp, color = White)
                }

                CodeView(
                    code =
                    """
                    Button(
                        onClick = { ToastUtils.show("按钮点击") },
                        border = BorderStroke(width = 2.dp, color = Orange),
                    ) {
                        Text(text = "设置了外边框的按钮", fontSize = 18.sp, color = White)
                    }
                    """.trimIndent()
                )

                Button(onClick = { ToastUtils.show("按钮点击") }, colors = CustomButtonColors()) {
                    Text(text = "自定义背景颜色", fontSize = 18.sp, color = White)
                }

                CodeView(
                    code = """
                    //定义按钮背景
                    class CustomButtonColors : ButtonColors {

                        @Composable
                        override fun backgroundColor(enabled: Boolean): State<Color> {
                            return rememberUpdatedState(if (enabled) Orange else TextLightGray)
                        }

                        @Composable
                        override fun contentColor(enabled: Boolean): State<Color> {
                            return rememberUpdatedState(if (enabled) White else Orange)
                        }
                    }
                    //定义按钮
                    Button(onClick = { ToastUtils.show("按钮点击") }, colors = CustomButtonColors()) {
                        Text(text = "自定义背景颜色", fontSize = 18.sp, color = White)
                    }
                    """.trimIndent()
                )
            }
        }
    }
}